package com.andoridtools.weidgt;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.andoridtools.R;

/**
 * popupwindow class
 */

public class PopWinBottom extends PopupWindow {
    WindowManager.LayoutParams params = null;

    public PopWinBottom(final Context context, final View popView, View displayView) {
        this.setOutsideTouchable(true);
        popView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = popView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        this.setContentView(popView);
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(R.style.take_photo_anim);

        this.showAtLocation(displayView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        params = ((Activity) context).getWindow().getAttributes();
        //Popupwindow out display setting alpha
        params.alpha = 0.7f;
        ((Activity) context).getWindow().setAttributes(params);

        this.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                params = ((Activity) context).getWindow().getAttributes();
                params.alpha = 1f;
                ((Activity) context).getWindow().setAttributes(params);
            }
        });
    }
}
